<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <p></p>
        <script>
            let p1 = new Promise((resolve, reject) => {
                resolve('成功了');
            });

            let p2 = new Promise((resolve, reject) => {
                resolve('success');
            });

            let p3 = Promise.reject('失败');

            Promise.all([p1, p2])
                .then((result) => {
                    console.log(result); //['成功了', 'success']
                })
                .catch((error) => {
                    console.log(error);
                });

            Promise.all([p1, p3, p2])
                .then((result) => {
                    console.log(result);
                })
                .catch((error) => {
                    console.log(error); // 失败了，打出 '失败'
                });

            // 一、Promise.all传递的参数可能不是Promise类型，可能不存在then方法。二、如果中间发生错误，应该直接返回错误，不执行后面操作。
            Promise.all_my_1 = function (promises) {
                let results = [];
                return new Promise(function (resolve) {
                    promises.forEach(function (promi) {
                        // 按顺序执行每一个Promise操作
                        val.then(function (res) {
                            results.push(res);
                        });
                    });
                    resolve(results);
                });
            };

            Promise.prototype.all_my = function (promises) {
                let results = [];
                let promiseCount = 0;
                let promisesLength = promises.length;
                return new Promise(function (resolve, reject) {
                    for (let promi of promises) {
                        Promise.resolve(promi).then(
                            function (res) {
                                promiseCount++;
                                // results.push(res);
                                results[i] = res;
                                // 当所有函数都正确执行了，resolve输出所有返回结果。
                                if (promiseCount === promisesLength) {
                                    return resolve(results);
                                }
                            },
                            function (err) {
                                return reject(err);
                            }
                        );
                    }
                });
            };

            function myPromiseAll(promises) {
                return new Promise((resolve, reject) => {
                    if (!Array.isArray(promises)) {
                        return Promise.reject(new Error('输入的参数必须是数组'));
                    }
                    const ans = [];
                    const promisesLen = promises.length;
                    const count = 0;
                    for (let index = 0; index < promises.length; index++) {
                        Promise.resolve(promises[index])
                            .then((value) => {
                                count++;
                                ans[index] = value;
                                if (count === promisesLen) {
                                    resolve(ans);
                                }
                            })
                            .catch((err) => {
                                reject(err);
                            });
                    }
                });
            }
        </script>
    </body>
</html>
